//
//  WTVisitorView.swift
//  WTSinaWeibo
//
//  Created by  Wellim on 16/6/25.
//  Copyright © 2016年  Wellim. All rights reserved.
//

import UIKit
import SnapKit

class WTVisitorView: UIView {
  
    // button按钮点击闭包
    var closure:(()->())?
    
    
  
   override init(frame: CGRect) {
     super.init(frame: frame)
    
    setUpUI()
    
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - 设置视图
    
    private func setUpUI(){
    
     backgroundColor = RGB(237, green: 237, blue: 237)
        
    //01- 添加控件
     addSubview(feedImageView)
     addSubview(maskImageView)
     addSubview(houseImageView)
     addSubview(messageLabel)
     addSubview(loginBtn)
     addSubview(registerBtn)
        
        
    // 02- 添加约束  
    //圈圈
        feedImageView.snp_makeConstraints { (make) in
          make.center.equalTo(self)
            
            
        }
   //遮罩
        maskImageView.snp_makeConstraints { (make) in
            
           make.center.equalTo(self)
        }
    //房子
       houseImageView.snp_makeConstraints { (make) in
         make.center.equalTo(self)
        }
        
    //文字
     messageLabel.snp_makeConstraints { (make) in
        
        make.width.equalTo(224)
        make.height.equalTo(36)
        make.top.equalTo(maskImageView.snp_bottom).offset(16)
        make.centerX.equalTo(maskImageView)
        
    }
    //登录
     loginBtn.snp_makeConstraints { (make) in
        
        make.size.equalTo(CGSize(width: 100, height: 36))
        make.left.equalTo(messageLabel)
        make.top.equalTo(messageLabel.snp_bottom).offset(16)
        
    }
      //注册
      registerBtn.snp_makeConstraints { (make) in
        make.size.equalTo(CGSize(width: 100, height: 36))
        make.right.equalTo(messageLabel)
        make.top.equalTo(loginBtn)
        
        
        }
    
        
        
    
    }
    
   // MARK: - 懒加载控件
    // 圆圈
    private lazy var feedImageView: UIImageView = UIImageView(imageName: "visitordiscover_feed_image_smallicon")
    // 遮罩
    private lazy var maskImageView: UIImageView = UIImageView(image: UIImage.init(named: "visitordiscover_feed_mask_smallicon"))
    // 房子 visitordiscover_feed_image_house
    
    private lazy var houseImageView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    
    // 文字
    private lazy  var messageLabel: UILabel = {
     
        let label = UILabel()
        label.textColor = ThemeColor
        label.text = "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜"
        label.numberOfLines = 0

        label.font = UIFont.systemFontOfSize(14)
        label.textAlignment = NSTextAlignment.Center
        
        return label
        
    }()
    
    //登录
    lazy var loginBtn:UIButton = {
    
     let button = UIButton()
     button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
      button.setTitle("登录", forState: UIControlState.Normal)
      button.setTitleColor(ThemeColor, forState: UIControlState.Normal)
        
      button.addTarget(self, action: #selector(WTVisitorView.bottonClick), forControlEvents: UIControlEvents.TouchUpInside)
      button.titleLabel?.font = UIFont.systemFontOfSize(14)
        
        
     return button
    
    }()
    //注册
    lazy var registerBtn:UIButton = {
     
        let button = UIButton()
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        button.setTitleColor(ThemeColor, forState: UIControlState.Normal)
        button.setTitle("注册", forState: UIControlState.Normal)
        button.titleLabel?.font = UIFont.boldSystemFontOfSize(14)
        button.addTarget(self, action: "bottonClick", forControlEvents: UIControlEvents.TouchUpInside)
        
         return button
    }()
    
    
    // MARK: - 监听事件
    @objc private func bottonClick(){
      //执行闭包
      closure?()
    
    }
    
    // MARK: - 设置动画
    func setUpAnim(){
    
    let anim = CABasicAnimation(keyPath: "transform.rotation")
        // 设置动画时间
        anim.duration = 10;
        // 设置tovalue
        anim.toValue = M_PI * 2
        // 动画次数
        anim.repeatCount = MAXFLOAT
        // 当我们app推到后台或者切换控制器的时候 动画默认完成
        anim.removedOnCompletion = false
         // 添加动画
        feedImageView.layer.addAnimation(anim, forKey: nil)
        
    }
    
    // MARK: - 供外界调用的方法(因为首页不需要再设置 因为该访客视图界面就是按照首页开发)
    
    func setUpVisitorViewInfo(imgName:String?,title:String?) {
        // 如果有值就代表不是主页
        if let img = imgName,let til = title {
            
            houseImageView.image = UIImage(named: img)
            
            messageLabel.text = til
            
            feedImageView.hidden = true
        }else{
            
          setUpAnim()
            
        }
        
    }
    
    
    

}
